﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;
using Avatar.ADService;
using System.Web.Caching;

public partial class UserControl_AD : System.Web.UI.UserControl
{
    string _adCategoryName;

    public string ADCategoryName
    {
        get
        {
            return _adCategoryName;
        }
        set
        {
            _adCategoryName = value;
        }
    }

    public ADCategory _adCategory = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            List<AD> _adList = new List<AD>();
            _adCategory = ADCategoryManager.GetADCategory(_adCategoryName);
            if (_adCategory == null) throw new Exception("广告分类名称设置不正确");

            if (Cache["ad" + _adCategory.ID.ToString()] == null)
            {
                _adList = ADManager.GetJobADList(_adCategoryName);
                Cache.Insert("ad" + _adCategory.ID.ToString(), _adList, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero);
            }
            _adList = (List<AD>)Cache["ad" + _adCategory.ID.ToString()];

            ListView1.DataSource = _adList;    
            ListView1.GroupItemCount = _adCategory.MaxColumn;
            ListView1.DataBind();
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "注意:" + _e.Message;
        }
    }
    protected string GetIconURL(string _url)
    {
        return string.IsNullOrEmpty(_url) ? "~/ad/jobad/normal.gif" : _url;
    }
    protected string GetCompanyName(string _user)
    {
        string _url=string.Empty;

        using (var _adc = DBHelper.GetDataContext())
        {
            var _compay = _adc.CompanyInformation.Where(i => i.User == _user).FirstOrDefault();
            if (_compay != null)
                _url = "CompanyJobList.aspx?Company=" + _compay.CompanyName;
            else
                _url = "#";
        }
        return _url;
    }
}
